package com.bwhx.eruler.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Buyer;
import com.bwhx.eruler.dao.BuyerDao;
import com.bwhx.eruler.util.MD5Utils;

/**
 * @Description:
 */
@Repository
public class BuyerDaoImpl extends BaseDaoImpl<Buyer> implements BuyerDao {
	public BuyerDaoImpl() {
		this.setNs("com.bwhx.eruler.mapper.BuyerMapper.");
	}
	
	@Override
	public Buyer findByBName(String getbName) {
		return getSqlSession().selectOne("com.bwhx.eruler.mapper.BuyerMapper.findByBName", getbName);
	}


	


	@Override
	public synchronized int insertSN() {
		
		
		Object selectOne = getSqlSession().selectOne("com.bwhx.eruler.mapper.BuyerMapper." + "getMaxSN");
		if(null!=selectOne && selectOne instanceof Integer){
			int i = (Integer)selectOne+1;
			getSqlSession().insert("com.bwhx.eruler.mapper.BuyerMapper." + "insertSN",i);
			return i;
		}
		return -1;
		 
	}


	@Override
	public void inputBuyer(Buyer s) {
		this.getSqlSession().update("com.bwhx.eruler.mapper.BuyerMapper." + "updateruler", s);
		
		
	}

	@Override
	public List<Buyer> list(Page p) {
		return getSqlSession().selectList("com.bwhx.eruler.mapper.BuyerMapper." + "list",p);

	}


	@Override
	public Integer count(Buyer s) {
		Object selectOne = getSqlSession().selectOne("com.bwhx.eruler.mapper.BuyerMapper." + "count",s);
		if(null!=selectOne && selectOne instanceof Integer){
			return (int)selectOne;
		}
		return 0;
	}


	

	@Override
	public List<Buyer> listAll() {
		return getSqlSession().selectList("com.bwhx.eruler.mapper.BuyerMapper." + "listAll");
	}


	@Override
	public Buyer findById(Integer oOKuGuanId) {
		
		return getSqlSession().selectOne("com.bwhx.eruler.mapper.BuyerMapper." + "findById",oOKuGuanId);

	}


	@Override
	public Buyer findByLoginName(String loginName) {
		
		return  getSqlSession().selectOne("com.bwhx.eruler.mapper.BuyerMapper." + "findByLoginName", loginName);
	}

	@Override
	public List<Buyer> findByName(String buyername) {
		return getSqlSession().selectList("com.bwhx.eruler.mapper.BuyerMapper." + "findByPartName",buyername);
	}

	@Override
	public void update(HashMap<String, Integer> map) {
		
		Buyer buyer = (Buyer)getSqlSession().selectOne("com.bwhx.eruler.mapper.BuyerMapper." + "get",map.get("id"));
		
		if(map.containsKey("number")){
			map.put("number", map.get("number")+buyer.getbRulerSum());
		}else{
			map.put("number", buyer.getbRulerSum());
		}
		map.put("number0", map.get("number0")+buyer.getbSum0Tape());
		map.put("number1", map.get("number1")+buyer.getbSum1Tape());
		map.put("number2", map.get("number2")+buyer.getbSum2Tape());
		map.put("number3", map.get("number3")+buyer.getbSum3Tape());

	
		getSqlSession().update("com.bwhx.eruler.mapper.BuyerMapper." + "updateBRulerSum", map);		
		
	}

	@Override
	public Buyer getBySDKID(String string) {
		
		return getSqlSession().selectOne("com.bwhx.eruler.mapper.BuyerMapper." + "getBySDKID",string);
	}

	@Override
	public Buyer findByBNameAndBCode(Buyer buyer) {
		return getSqlSession().selectOne("com.bwhx.eruler.mapper.BuyerMapper." + "findByBNameAndBCode",buyer);
	}

	

	
	
}
